﻿using B.S.RbacData.Domain.Traceabilitys;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Read.API.Applications.Commands.Traceabilitys.Slaughters;
using MediatR;

namespace B.S.RbacData.Read.API.Applications.CommandHandlters.Traceabilitys.Slaughters
{


    /// <summary>
    /// 获取屠宰信息命令处理
    /// </summary>
    public class GetSlaughterCorporateCommandHandlter : IRequestHandler<GetSlaughterCorporateCommand, ApiResult<List<SlaughterCorporateModel>>>
    {
        /// <summary>
        /// 屠宰仓储
        /// </summary>
        private readonly IBaseRepository<SlaughterCorporateModel> slaughterRep;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="slaughterRep">屠宰仓储</param>
        public GetSlaughterCorporateCommandHandlter(IBaseRepository<SlaughterCorporateModel> slaughterRep)
        {
            this.slaughterRep = slaughterRep;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>

        public async Task<ApiResult<List<SlaughterCorporateModel>>> Handle(GetSlaughterCorporateCommand request, CancellationToken cancellationToken)
        {

            //根据溯源编号查询屠宰信息

            var slaughter = slaughterRep.GetAll().Where(x => x.ProductId == request.ProductId).OrderByDescending(x => x.Creatime).ToList();


            return new ApiResult<List<SlaughterCorporateModel>>
            {

                code = ResultEnumCode.Ok,
                data = slaughter,
                message = "获取成功"

            };
        }
    }




}
